package cn.yunyichina.reservation.controller.console;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.controller.BaseController;
import cn.yunyichina.reservation.util.RequestUtil;
import cn.yunyichina.reservation.vo.MechanismVo;
import cn.yunyichina.utils.convert.JsonUtils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by JoeZ on 2016/8/24.
 */
@RestController
@RequestMapping("console")
public class ConsoleMechanismController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(ConsoleLoginController.class);


    /* 获取子系统服务 */
    private static final String RESERVATION_CONSOLE_MECHANISM_ADD = "yyt.reservation.mechanism.add";
    private static final String RESERVATION_CONSOLE_MECHANISM_DELETE = "yyt.reservation.mechanism.delete";
    private static final String RESERVATION_CONSOLE_MECHANISM_UPDATE = "yyt.reservation.mechanism.update";
    private static final String RESERVATION_CONSOLE_MECHANISM_FIND = "yyt.reservation.mechanism.find";
    private static final String RESERVATION_CONSOLE_MECHANISM_LIST = "yyt.reservation.mechanism.list";
    private static final String RESERVATION_CONSOLE_MECHANISM_STOP = "yyt.reservation.mechanism.stop";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("addMechanism")
    public HttpResponse add(HttpServletRequest request) {

        HttpResponse httpResponse = new HttpResponse();
        Map<String, Object> map = new HashMap<>();

        try{
            String id = PKGenerator.generateId();
            String name = request.getParameter("name");
            String account = request.getParameter("account");
            String grade = request.getParameter("grade");
            String serial_num = PKGenerator.generateId();
            String address = request.getParameter("address");
            String province = request.getParameter("province");
            String province_code = request.getParameter("province_code");
            String city = request.getParameter("city");
            String city_code = request.getParameter("city_code");
            String contact_name = request.getParameter("contact_name");
            String contact_phone = request.getParameter("contact_phone");
            String status = request.getParameter("status");
            String enable_time = request.getParameter("enable_time");
            String disable_time = request.getParameter("disable_time");
            String remark = request.getParameter("remark");

            map.put("id", id);
            map.put("name", name);
            map.put("account", account);
            map.put("grade", grade);
            map.put("serial_num", serial_num);
            map.put("address", address);
            map.put("province", province);
            map.put("province_code", province_code);
            map.put("city", city);
            map.put("city_code", city_code);
            map.put("contact_name", contact_name);
            map.put("contact_phone", contact_phone);
            map.put("status", status);
            map.put("enable_time", enable_time);
            map.put("disable_time", disable_time);
            map.put("remark", remark);

        }catch (Exception e){
            logger.info("参数解析异常",e.getMessage());
            httpResponse.failure(
                    ResponseEnum.FAILURE.getCode(),
                    ResponseEnum.FAILURE.getMessage());
        }
        Response serviceResponse = ReservationService.callHumpFromInternal(RESERVATION_CONSOLE_MECHANISM_ADD, Jackson.getBaseJsonData(map));
        if("0".equals(serviceResponse.getResultCode())){
            return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
        }else{
            httpResponse.failure(
                    ResponseEnum.ILLEGAL_PARAMETER.getCode(),
                    ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }

        return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
    }



    @RequestMapping("deleteMechanism")
    public HttpResponse delete(HttpServletRequest request, HttpServletResponse response) {
        HttpResponse httpResponse = new HttpResponse();
        String id = request.getParameter("id");
        if(StringUtils.isNotBlank(id)){
            Response serviceResponse = ReservationService.callHumpFromInternal(RESERVATION_CONSOLE_MECHANISM_DELETE, id);
            if("0".equals(serviceResponse.getResultCode())){
            }else{
                return httpResponse.failure(
                        ResponseEnum.ILLEGAL_PARAMETER.getCode(),
                        ResponseEnum.ILLEGAL_PARAMETER.getMessage());
            }

        }else{
            return httpResponse.failure(
                    ResponseEnum.ILLEGAL_PARAMETER.getCode(),
                    ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }
        return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
    }



    @RequestMapping("updateMechanism")
    public HttpResponse update(HttpServletRequest request, HttpServletResponse response) {

        HttpResponse httpResponse = new HttpResponse();
//        Map<String, Object> map = new HashMap<>();
        Map<String, String> map = RequestUtil.getAllParameters(request);
        try{
//            String id =  request.getParameter("id");
//            String name = request.getParameter("name");
//            String grade = request.getParameter("grade");
//            String address = request.getParameter("address");
//            String contact_name = request.getParameter("contactName");
//            String contact_phone = request.getParameter("contactPhone");
//
//            map.put("id", id);
//            map.put("name", name);
//            map.put("grade", grade);
//            map.put("address", address);
//            map.put("contactName", contact_name);
//            map.put("contactPhone", contact_phone);

            
        }catch (Exception e){
            logger.info("参数解析异常",e.getMessage());
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        Response serviceResponse = ReservationService.callHumpFromInternal(RESERVATION_CONSOLE_MECHANISM_UPDATE, JsonUtils.toJsonString(map));
        if("0".equals(serviceResponse.getResultCode())){

        }else{
            return httpResponse.failure(ResponseEnum.ILLEGAL_PARAMETER.getCode(), ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }

        return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
    }


    @RequestMapping("findMechanism")
    public HttpResponse find(HttpServletRequest request, HttpServletResponse response) {
        HttpResponse httpResponse = new HttpResponse();
        String id = request.getParameter("id");
        if(StringUtils.isNotBlank(id)){
            Response serviceResponse = ReservationService.callHumpFromInternal(RESERVATION_CONSOLE_MECHANISM_FIND, id);
            if("0".equals(serviceResponse.getResultCode())){
                MechanismVo vo = Jackson.getBeanJsonData(serviceResponse.getResult(), MechanismVo.class);
                if(vo != null){
                    httpResponse.setData(vo);
                }
                return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
            }else{
                httpResponse.failure(
                        ResponseEnum.ILLEGAL_PARAMETER.getCode(),
                        ResponseEnum.ILLEGAL_PARAMETER.getMessage());
            }

        }else{
            httpResponse.failure( ResponseEnum.ILLEGAL_PARAMETER.getCode(),ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }
        return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
    }


    @RequestMapping("listMechanism")
    public HttpResponse list(HttpServletRequest request, HttpServletResponse response) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, Object> map = new HashMap<>();
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        //获取索引参数
        String name = request.getParameter("name");
        String provinceCode = request.getParameter("provinceCode");
        String cityCode = request.getParameter("cityCode");

        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("cityCode",cityCode);
        map.put("provinceCode",provinceCode);
        map.put("name",name);

        Response serviceResponse = ReservationService.callHumpFromInternal(RESERVATION_CONSOLE_MECHANISM_LIST, Jackson.getBaseJsonData(map));
        if("0".equals(serviceResponse.getResultCode())){
            List<MechanismVo> datas = Jackson.getBeanJsonData(serviceResponse.getResult(), List.class);
            httpResponse.setData(datas);

        }else{
            httpResponse.failure(
                    ResponseEnum.ILLEGAL_PARAMETER.getCode(),
                    ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }
        return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
    }


    @RequestMapping("stopMechanism")
    public HttpResponse stop(HttpServletRequest request, HttpServletResponse response) {

        HttpResponse httpResponse = new HttpResponse();
        Map<String, Object> map = new HashMap<>();

        String id =  request.getParameter("id");
        String status = request.getParameter("status");

        map.put("id", id);
        map.put("status", status);

        Response serviceResponse = ReservationService.callHumpFromInternal(RESERVATION_CONSOLE_MECHANISM_STOP, Jackson.getBaseJsonData(map));
        if("0".equals(serviceResponse.getResultCode())){

        }else{
            return httpResponse.failure(ResponseEnum.ILLEGAL_PARAMETER.getCode(), ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }

        return httpResponse.success(ResponseEnum.SUCCESS.getMessage());
    }
}
